package at.ac.univie.bi.transformation.service.output;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import at.ac.univie.bi.transformation.model.output.AbstractOutputEntity;
import at.ac.univie.bi.transformation.model.output.PCP;
import at.ac.univie.bi.transformation.persistence.DBHandler;

public class PCPService extends AbstractOutputEntityService{

	@Override
	public int save(AbstractOutputEntity element) {
		int newid = 0;
		if(element instanceof PCP)
		{
			PCP pcp = (PCP)element;
			String sqlstatement = "INSERT INTO ProcesscaseParameter (parameterId, processcaseId) VALUES ("+pcp.getParameterid()+","+pcp.getProcesscaseid()+")";
			newid = executeInsertStatement(sqlstatement);
		}
		
		return newid;
	}
	
	public int executeInsertStatement(String sqlstatement){
	  int newid = 0;
	  
	  try
      {
		  String generatedColumns[] = {"ID"};
          PreparedStatement pstmt =  DBHandler.getIntegrationDB().prepareStatement(sqlstatement, generatedColumns);
          pstmt.executeUpdate();
          
          ResultSet rs = pstmt.getGeneratedKeys();
          while (rs.next()){
        	    newid = rs.getInt(1);
          }	
      } 
	  catch (Exception e) {	
			System.err.println("Error: " + e.getMessage());
	  } 
	  
	  return newid;
  	}

}
